The IDL_String class contains static methods that are available for all IDL strings. In addition, because IDL_String is a subclass of IDL_Variable, all of the IDL_Variable methods are also available.
The IDL_String::CapWords method capitalizes the beginning letter of all words within the string.
Create a sentence and print the result of CapWords on that sentence:
str = "hello fellow IDL user."
PRINT, str.CapWords( )
IDL prints:
Hello Fellow IDL User.
Pass in a character for the space argument, and include the optional keywords:
str = "hello_fellow_IDL_user."
PRINT, str.CapWords('_', /FOLD_CASE, /IGNORE_FIRST)
IDL prints:
hello_Fellow_IDL_User.
Result = var.CapWords( [Char] [, /FOLD_CASE] [, /IGNORE_FIRST] )
A string with a value containing capitalized characters as specified by the given argument or keywords. If var is an array then Result is a string array of the same dimensions.
A character string designating the space character that will capitalize each character immediately following the space character. The default is a single space character.
Set this keyword to ignore case when searching for space characters.
Set this keyword to ignore the capitalization of the first character.
The IDL_String::CharAt method returns a character from the string.
Create a string and print the result given an index:
str = "IDL"
; Print the character at each index.
FOR i=0, str.StrLen( )-1 DO PRINT, str.CharAt(i)
IDL prints:
I
D
L
Result = var.CharAt( Index )
A string containing the character found at the given index. If var is an array then Result is a string array of the same dimensions.
The index of the character to return.
None.
None.
The IDL_String::Compress method removes all spaces and tabs within a string. This is equivalent to calling STRCOMPRESS with the REMOVE_ALL keyword.
Create a string with spaces:
str = " I D L is C o o l ! "
PRINT, str.Strlen( )
newstr = str.Compress()
; Print the new string and its length.
PRINT, newstr, newstr.Strlen( )
IDL prints:
20
IDLisCool!
10
Result = var.Compress( )
The string with all white space removed. If var is an array then the result is a string array of the same dimensions.
None.
None.
The IDL_String::Contains method determines whether the string contains a given substring.
Determine whether each element of a string array contains a specific substring:
str = ['code.pro', 'image.jpg', 'file.txt']
PRINT, str.Contains('.pro')
IDL prints:
1 0 0
Result = var.Contains( Substring, /FOLD_CASE )
The result is a boolean value 1 (true) if the string contains the substring, or 0 otherwise. If var is an array then Result is a byte array of the same dimensions.
The string to compare against.
Set this keyword to ignore case when doing comparisons.
The IDL_String::EndsWith method determines whether the string ends with a given string.
Create a sentence and print the result of EndsWith on that sentence:
str = "hello fellow IDL user..."
; Check if the string ends with 3 periods.
PRINT, str.EndsWith('...')
IDL prints:
1
Result = var.EndsWith( String [, /FOLD_CASE] )
A boolean value of 0 (false) or 1 (true). If var is an array then Result is a byte array of the same dimensions.
The string to compare against the end of the string variable.
Set this keyword to ignore case when doing comparisons.
The IDL_String::Extract method extracts the first substring that matches a regular expression.
Create a sentence and extract text out of it:
str = "Hello fellow IDL user."
PRINT, str.Extract('f[a-z]{5}')
IDL prints:
fellow
Result = var.Extract( String [, /FOLD_CASE] [, /SUBEXPR] )
A string containing the extracted text. If var is an array then Result is a string array of the same dimensions.
The regular expression to match against var. See Learning About Regular Expressions for details.
Set this keyword to ignore case when doing comparisons.
By default, IDL_STRING::Extract only returns a single string containing the overall match (or a string array if your variable was an array). Setting SUBEXPR causes it to return an array containing the overall match as well as any subexpression matches. A subexpression is any part of a regular expression written within parentheses. For example, the regular expression "abc+" has no sub-expressions while the regular expression "(a)(b)(c+)" has 3 subexpressions. For the regular expression "(a)(b)(c+)", the result will therefore be a 4-element vector - 1 for the overall match and 3 more for the subexpressions. If a subexpression participated in the match several times, the reported substring is the last one that matched.
Note: If your variable is an array then the result will be an array of one higher dimension, where the first dimension contains the sub expressions. For example, if var was a [10, 5] string array, and you use the regular expression "(a)(b)(c+)", then the result will be a [4, 10, 5] string array.
The IDL_String::IndexOf method returns the index of a character or sub-string within the string.
Create a string array with some data:
str = ["iaa", "aIa", "aai", "aaa"]
; Find the indexes for the letter i.
PRINT, str.IndexOf('i', /FOLD_CASE)
IDL prints:
0 1 2 -1
Result = var.IndexOf( Substring [, Start] [, /FOLD_CASE] )
An integer giving the index of the first match of the substring. If the substring is not found then -1 is returned. If var is an array then the result is an integer array of the same dimensions.
A string giving the character or substring to search for.
An optional index at which to start searching. If Start is beyond the string's length then -1 is returned.
Set this keyword to ignore case when doing comparisons.
The IDL_String::Insert method inserts text at a specified index within the string.
Create a sentence and print the result after insert:
str = "Hello IDL user."
; Insert at index 5.
PRINT, str.Insert(" fellow", 5)
IDL prints:
Hello fellow IDL user.
Extend the sentence and use the FILL keyword:
; The FILL keyword will add the space for us.
str = str.Insert("How are you?", 16, FILL=' ')
PRINT, str
IDL prints:
Hello IDL user. How are you?
Result = var.Insert( String [, Index] [, FILL_CHARACTER=character] )
The new string. If var is an array then Result is a string array of the same dimensions.
If only the string argument is given, the method will insert the string at the end.
An integer giving the index at which to insert the string.
Note: By default, if the index is not a valid index within var then the string will not be inserted; however, you can use the FILL keyword to insert fill characters to allow the new string to be inserted.
Set this keyword to a single string character that is used to fill empty space if the supplied index is outside of the string's length.
The IDL_String::Join method collapses a one-dimensional string array into a single merged string or a multi-dimensional string array into an array where the first dimension has been merged.
Create a sentence to split:
str = ["Hello", "fellow", "IDL", "user."]
; Join the string array into a single string.
PRINT, str.Join('-')
IDL prints:
Hello-fellow-IDL-user.
Result = var.Join( [Delimiter] )
The merged string. If var is a multi-dimensional string array then Result is a string array of one less dimension since the first dimension has been collapsed.
An optional separator string to use between the joined strings. If Delimiter is not specified, an empty string is used.
None.
The IDL_String::LastIndexOf method returns the last index of a given substring within the string.
Create a string array with some data:
str = ["Apples", "Oranges", "Bananas", "Kiwis"]
; Find the last indexes for the letter a.
PRINT, str.LastIndexOf('a', /FOLD_CASE)
IDL prints:
0 2 5 -1
Result = var.LastIndexOf( Substring [, /FOLD_CASE] )
An integer giving the index of the last match of the substring. If the substring is not found then -1 is returned. If var is an array then the result is an integer array of the same dimensions.
The character or string to search for.
Set this keyword to ignore case when doing comparisons.
The IDL_String::Matches method determines whether the string matches a regular expression.
Use a regular expression to determine if a string contains a valid integer number:
str = ['123', '-123', ' 4 ', '123abc']
; line begin + optional whitespace + optional negative sign
; + some digits + optional whitespace + line end
result = str.Matches('^ *-?[0-9]+ *$')
PRINT, result
IDL prints:
1 1 1 0
Result = var.Matches( String, /FOLD_CASE )
A boolean value of 1 if the given string matches the string, or 0 otherwise. If var is an array then the result is a byte array of the same dimensions.
A regular expression to be used for matching. See Learning About Regular Expressions for details.
Set this keyword to ignore case when doing comparisons.
The IDL_String::Remove method removes part of a string given the start and end index.
Create a string and remove a section from it:
str = 'An example IDL string'
PRINT, str.Remove(2, 9)
IDL prints:
An IDL string
Create a string and remove the last 4 characters:
str = 'myfilename.pro'
PRINT, str.Remove(-4)
IDL prints:
myfilename
Result = var.Remove( StartIndex [, EndIndex] )
A new string containing the original string, minus the part that was removed. If var is an array then the result is a string array of the same dimensions.
An integer giving the starting position. If StartIndex is negative then it represents an index from the end. If StartIndex is beyond the end then nothing is removed.
An integer giving the ending position. This includes the character at that position. If EndIndex is negative then it represents an index from the end. If not specified or beyond the end then EndIndex will be set to the string length – 1.
None.
The IDL_String::Replace method replaces text within the string with new text.
Create a string to perform a replace on:
str = "Hello fellow IDL user."
; Print the sentence using replace
PRINT, str.Replace('fellow', 'friendly')
IDL prints:
Hello friendly IDL user.
Result = var.Replace( StringFrom, StringTo [, /FOLD_CASE] )
A string variable containing the new string. If var is an array then the result is a string array of the same dimensions.
Set this argument to the text you want to replace within the variable.
Set this argument to the text you want to be inserted in place of StringFrom.
Set this keyword to ignore case when doing comparisons.
The IDL_String::Reverse method reverses the string.
Create a string to perform a reverse on:
str = "Racecar"
; Print using reverse.
PRINT, str.Reverse( )
IDL prints:
racecaR
Result = var.Reverse( )
The new string. If var is an array then the result is a string array of the same dimensions.
None.
None.
The IDL_String::Split method splits the string into substrings according to a specified regular expression.
Create a sentence to split:
str = "Hello fellow IDL user."
; Split using spaces and print the first element.
var = str.Split(' ')
PRINT, var[0]
IDL prints:
Hello
Result = var.Split( String [, /FOLD_CASE] )
A string array with values after the split.
A regular expression specifying where to perform the split. See Learning About Regular Expressions for details.
Set this keyword to ignore case when doing comparisons.
The IDL_String::StartsWith method determines whether the string starts with the supplied string.
Create a sentence to use StartWith on:
str = "Hello fellow IDL user."
; Print the result of StartsWith
PRINT, str.StartsWith('h')
; Perform the same with /FOLD_CASE
PRINT, str.StartsWith('h', /FOLD_CASE)
IDL prints:
0
1
Result = var.StartsWith( String [, /FOLD_CASE] )
A boolean value of 1 (true) or 0 (false). If var is an array then the result is a byte array of the same dimensions.
The string to check against the beginning of the variable.
Set this keyword to ignore case when doing comparisons.
The IDL_String::Strlen method determines the string length.
Create a sentence and check its length with Strlen:
str = "Hello fellow IDL user."
; Print the length of the string
PRINT, str.Strlen( )
; Now split the string and print the lengths
PRINT, (str.Split(' ')).Strlen()
IDL prints:
22
5 6 3 5
Result = var.StrLen( )
The length of the string. If var is an array then the result is an integer array of the same dimensions.
None.
None.
The IDL_String::Substring method returns a portion of the string given the start and end index.
Create a string and return only a portion of it:
str = 'An-IDL-string'
PRINT, str.Substring(3, 5)
IDL prints:
IDL
Now just supply the start index, and return the characters from that index to the end:
str = 'An-IDL-string'
PRINT, str.Substring(3)
IDL prints:
IDL-string
Finally, use a negative index to return characters from the end:
str = 'myfilename.pro'
PRINT, str.Substring(-3)
IDL prints:
pro
Result = var.Substring( StartIndex [, EndIndex] )
A string containing the specified substring. If var is an array then the result is a string array of the same dimensions.
An integer giving the starting position. If StartIndex is negative then it represents an index from the end. If StartIndex is beyond the end then an empty string is returned.
An integer giving the ending position. This includes the character at that position. If EndIndex is negative then it represents an index from the end. If not specified or beyond the end then EndIndex will be set to the string length – 1. If EndIndex is less than StartIndex then an empty string is returned.
None
The IDL_String::ToByte method converts the string to a byte array.
Create a string and convert it to a byte array:
str = 'IDL'
; Print the byte values for each character
PRINT, str.ToByte( )
; Same thing but for a multi-dimensional array
PRINT, (['IDL', 'is', 'fun!']).ToByte()
IDL prints:
73 68 76
73 68 76 0
105 115 0 0
102 117 110 33
Result = var.ToByte( )
An array of byte values representing the characters of the string. If var is an array then the result is a byte array of one-higher dimension. The first dimension will have a length equal to the longest string in the array.
None.
None.
The IDL_String::ToLower method converts the string to all lowercase characters.
str = "Hello Fellow IDL User."
PRINT, str.ToLower( )
IDL prints:
hello fellow idl user.
Result = var.ToLower( )
A string with all characters converted to lowercase. If var is an array then the result is a string array of the same dimensions.
None.
None.
The IDL_String::ToUpper method converts the string to all uppercase characters.
str = "Hello Fellow IDL User."
PRINT, str.ToUpper( )
IDL prints:
HELLO FELLOW IDL USER.
Result = var.ToUpper( )
A string with all characters converted to uppercase. If var is an array then the result is a string array of the same dimensions.
None.
None.
The IDL_String::Trim method removes any leading and trailing spaces or tabs from the string. This is equivalent to calling STRTRIM with Flag = 2.
Create a string with spaces before and after text:
str = " IDL is Cool! "
PRINT, str.Strlen( )
trim_str = str.Trim()
; Print the trimmed string and its length
PRINT, trim_str, trim_str.Strlen( )
IDL prints:
14
IDL is Cool!
12
Result = var.Trim( )
The string with leading and trailing spaces and tabs removed. If var is an array then the result is a string array of the same dimensions.
None.
None.
|
8.4 |
Introduced |
Static Methods and Attributes, Variable Attributes, IDL_Integer, IDL_Number, IDL_Pointer, IDL_Variable